package com.ruoyi.project.system.lotto.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.lotto.domain.ResObject;
import com.ruoyi.project.system.lotto.domain.UserLogs;
import com.ruoyi.project.system.lotto.service.UserLogsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户记录控制器
 */
@Api("用户信息管理")
@RequestMapping("/userLogs")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
@RestController
public class UserLogController {
    @Autowired
    private UserLogsService userService;

    /**
     * 保存用户信息
     * @param userLogs
     * @return
     */
    @CrossOrigin
    @Log(title = "用户登记", businessType = BusinessType.INSERT)
    @PostMapping("save")
    @ResponseBody
    public ResObject save(@RequestBody UserLogs userLogs) {
        try {
            if (StrUtil.isBlank(userLogs.getOpenid())) {
                return ResObject.error("openid 不可为空");
            }

            UserLogs user = userService.getByOpenid(userLogs.getOpenid());
            if (user != null) {
                return ResObject.error("用户已注册");
            }
            userService.insert(userLogs);
            return ResObject.success("登记成功");
        } catch (Exception e) {
            return ResObject.error("登记失败,请重新进入");
        }
    }

    /**
     * 更新用户信息
     * @param userLogs
     * @return
     */
    @CrossOrigin
    @Log(title = "用户登记", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    @ResponseBody
    public ResObject update(@RequestBody UserLogs userLogs) {
        try {
            if (StrUtil.isBlank(userLogs.getOpenid())) {
                return ResObject.error("openid 不可为空");
            }

            userService.updateByPrimaryKeySelective(userLogs);
            return ResObject.success("更新成功");
        } catch (Exception e) {
            return ResObject.error("更新失败");
        }
    }

    /**
     * 个人信息详情
     * @return
     */
    @CrossOrigin
    @PostMapping("detail")
    @ResponseBody
    public ResObject detail(@RequestBody UserLogs userLogs) {
        if (StrUtil.isBlank(userLogs.getOpenid())) {
            return ResObject.error("openid 不可为空");
        }

        UserLogs user = userService.getByOpenid(userLogs.getOpenid());
        return ResObject.success("查询成功", user);
    }
}
